package com.example.demo.config;

import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.apache.commons.lang3.RandomStringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class IMCommandParameters {

    /**
     * 添加账号
     * @param userId 用户ID
     * @param nickname 用户昵称
     * @param faceUrl 用户头像
     * @return 参数
     */
    public Map<String, Object> addAccount(String userId, String nickname, String faceUrl) {
        return IMOperations.dataBuilder()
            .put("Identifier", userId)
            .put("Nick", nickname)
            .put("FaceUrl", faceUrl)
            .buildData();
    }

    /**
     * 批量添加账号
     * @param userIds 用户ID列表
     * @return 参数
     */
    public Map<String, Object> addMultiAccount(List<String> userIds) {
        return IMOperations.dataBuilder()
            .put("Accounts", userIds)
            .buildData();
    }

    /**
     * 检测账号状态（可以检测多个）
     * @param userIds 用户ID列表
     * @return 参数
     */
    public Map<String, Object> checkAccount(List<String> userIds) {
        return IMOperations.dataBuilder()
            .put("CheckItem", userIds.parallelStream().map(userId -> IMOperations.dataBuilder().put("UserID", userId).buildData()).collect(Collectors.toList()))
            .buildData();
    }

    /**
     * 删除账号
     * @param userIds 用户ID列表
     * @return 参数
     */
    public Map<String, Object> deleteAccount(List<String> userIds) {
        return IMOperations.dataBuilder()
            .put("DeleteItem", userIds.parallelStream().map(userId -> IMOperations.dataBuilder().put("UserID", userId).buildData()).collect(Collectors.toList()))
            .buildData();
    }

    /**
     * 失效用户的UserSig
     * @param userId 用户ID
     * @return 参数
     */
    public Map<String, Object> invalidateUserSig(String userId) {
        return IMOperations.dataBuilder()
            .put("Identifier", userId)
            .buildData();
    }

    /**
     * 获取用户在线状态 一次最多查询500个 用户 的状态
     * @param userIds 用户ID列表
     * @return 参数
     */
    public Map<String, Object> queryUserState(List<String> userIds) {
        return IMOperations.dataBuilder()
            .put("To_Account", userIds)
            .buildData();
    }

    /**
     * 发送文本消息
     * @param fromAccount 从哪个账号
     * @param toAccount 发往哪个账号
     * @param text 消息内容
     * @return 参数
     */
    public Map<String, Object> sendTextMessage(String fromAccount, String toAccount, String text) {
        return IMOperations.dataBuilder()
            .put("SyncOtherMachine", 1)
            .put("From_Account", fromAccount)
            .put("To_Account", toAccount)
            .put("MsgLifeTime", 604800)
            .put("MsgRandom", Integer.parseInt(RandomStringUtils.randomNumeric(8)))
            .put("MsgTimeStamp", new Date().getTime() / 1000)
            .put("MsgBody", Lists.newArrayList(
                IMOperations.dataBuilder()
                    .put("MsgType", "TIMTextElem")
                    .put("MsgContent", IMOperations.dataBuilder()
                        .put("Text", text)
                        .buildData()
                    )
                    .buildData()
                )
            )
            .buildData();
    }

    /**
     * 发送自定义消息
     * @param fromAccount 从哪个账号
     * @param toAccount 发往哪个账号
     * @param data 自定义内容 一般是JSON字符串
     * @return 参数
     */
    public Map<String, Object> sendCustomMessage(String fromAccount, String toAccount, String data) {
        return IMOperations.dataBuilder()
            .put("SyncOtherMachine", 1)
            .put("From_Account", fromAccount)
            .put("To_Account", toAccount)
            .put("MsgLifeTime", 604800)
            .put("MsgRandom", Integer.parseInt(RandomStringUtils.randomNumeric(8)))
            .put("MsgTimeStamp", new Date().getTime() / 1000)
            .put("MsgBody", Lists.newArrayList(
                IMOperations.dataBuilder()
                    .put("MsgType", "TIMCustomElem")
                    .put("MsgContent", IMOperations.dataBuilder()
                        .put("Data", data)
                        .put("Desc", "")
                        .put("Ext", "")
                        .buildData()
                    )
                    .buildData()
                )
            )
            .buildData();
    }


    /**
     * 拉取用户信息
     * @param userIds 用户ID列表
     * @return 参数
     */
    public Map<String, Object> queryUserInfo(List<String> userIds) {
        return IMOperations.dataBuilder()
            .put("To_Account", userIds)
            .put("TagList", Lists.newArrayList(
                "Tag_Profile_IM_Nick",
                "Tag_Profile_IM_Gender",
                "Tag_Profile_IM_BirthDay",
                "Tag_Profile_IM_Location",
                "Tag_Profile_IM_SelfSignature",
                "Tag_Profile_IM_AllowType",
                "Tag_Profile_IM_Language",
                "Tag_Profile_IM_Image",
                "Tag_Profile_IM_MsgSettings",
                "Tag_Profile_IM_AdminForbidType",
                "Tag_Profile_IM_Level",
                "Tag_Profile_IM_Role"
            ))
            .buildData();
    }

    @Getter
    @AllArgsConstructor
    public enum Gender {
        MALE("Gender_Type_Male"), FEMALE("Gender_Type_Female");
        private String value;
    }

    /**
     * 设置用户信息
     * @param userId 用户ID
     * @param nickname 昵称（姓名）
     * @param gender 性别
     * @param birthday 生日 格式：19960328
     * @param avatar 头像
     * @param location 地址
     * @param desc 简介
     * @return 参数
     */
    public Map<String, Object> setUserInfo(String userId, String nickname, Gender gender, int birthday, String avatar, String location, String desc) {
        return IMOperations.dataBuilder()
            .put("From_Account", userId)
            .put("ProfileItem", Lists.newArrayList(
                IMOperations.dataBuilder()
                    .put("Tag", "Tag_Profile_IM_Nick")
                    .put("Value", nickname)
                    .buildData(),
                IMOperations.dataBuilder()
                    .put("Tag", "Tag_Profile_IM_Gender")
                    .put("Value", gender.getValue())
                    .buildData(),
                IMOperations.dataBuilder()
                    .put("Tag", "Tag_Profile_IM_BirthDay")
                    .put("Value", birthday)
                    .buildData(),
                IMOperations.dataBuilder()
                    .put("Tag", "Tag_Profile_IM_Location")
                    .put("Value", location)
                    .buildData(),
                IMOperations.dataBuilder()
                    .put("Tag", "Tag_Profile_IM_Image")
                    .put("Value", avatar)
                    .buildData(),
                IMOperations.dataBuilder()
                    .put("Tag", "Tag_Profile_IM_SelfSignature")
                    .put("Value", desc)
                    .buildData()
            ))
            .buildData();
    }

    @Getter
    @AllArgsConstructor
    public enum ChatType {
        C2C("C2C"), GROUP("Group");
        private String value;
    }

    /**
     * 下载历史消息
     * @param chatType 消息类型
     * @param msgTime 消息时间区间 格式：2020021222 表示下载 2020-02-12 22:00 - 22:59 时间段的消息
     * @return 参数
     */
    public Map<String, Object> downloadHistoricMessages(ChatType chatType, String msgTime) {
        return IMOperations.dataBuilder()
            .put("ChatType", chatType.getValue())
            .put("MsgTime", msgTime)
            .buildData();
    }

    /**
     * 创建群聊
     * @param groupId 群ID
     * @param name 群名称
     * @param introduction 群简介
     * @param notification 群通知
     * @param avatar 群头像
     * @param memberIds 群成员
     * @return 参数
     */
    public Map<String, Object> createGroup(String groupId, String name, String introduction, String notification, String avatar, List<String> memberIds) {
        return IMOperations.dataBuilder().put("Type", "ChatRoom")
            .put("GroupId", groupId)
            .put("Name", name)
            .put("Introduction", introduction)
            .put("Notification", notification)
            .put("FaceUrl", avatar)
            .put("MaxMemberCount", 6000)
            .put("ApplyJoinOption", "FreeAccess")
            .put("MemberList", memberIds.parallelStream()
                .map(memberId -> IMOperations.dataBuilder().put("Member_Account", memberId).buildData())
                .collect(Collectors.toList()))
            .buildData();
    }

    /**
     * 解散群组
     * @param groupId 群组ID
     * @return 参数
     */
    public Map<String, Object> destroyGroup(String groupId) {
        return IMOperations.dataBuilder()
            .put("GroupId", groupId)
            .buildData();
    }
}
